<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    // TODO 1. foo1 => foo2 
    const foo1 = {
      'A': 1,
      "B.A": 2,
      "B.B": 4,
      "CC.D.E": 3,
      "CC.D.F": 5
    }
    const foo2 = {
      'A': 1,
      "B": {
        "A": 2,
        "B": 4
      },
      "CC": {
        "D": {
          "E": 3,
          "F": 5
        }
      }
    }
    function method(obj) {
      let res = {}
      // 取出key
      Object.keys(obj).forEach(key => {
        // console.log(key);
        let value = obj[key]
        let keyArr = key.split('.')
        // console.log(keyArr);
        process(keyArr, res, value)
      })

      return res
      /**
       * keyArr: [A] [B,A] [B,B]
       * */
      function process(keyArr, res, value) {
        const len = keyArr.length
        if (len === 1) {
          // 只有一个层级 ['A']
          res[keyArr[0]] = value
        } else {
          // 多个层级 ['B', 'A'] || ['B', 'B'] || [CC, D ,E]
          if (!res.hasOwnProperty(keyArr[0])) {
            // 无B属性
            res[keyArr[0]] = {}
          }
          deep(res[keyArr[0]], 1, value, len, keyArr)

        }

      }

      function deep(curr, index, value, len, keyArr) {
        if (index === len - 1) {
          curr[keyArr[index]] = value
          return
        }
        if (!curr.hasOwnProperty(keyArr[index])) {
          // 无B属性
          curr[keyArr[index]] = {}
        }
        deep(curr[keyArr[index]], index + 1, value, len, keyArr)
      }
    }

    console.log(method(foo1));

    //  TODO2.实现一个m叉树结构转数组结构的方法
    function treeToArray(root) {
      // 存放结果
      let res = []

      // 1.第一层级
      let temp = {}
      temp.id = root.id
      temp.name = root.name
      temp.pid = root.pid
      res.push(temp)

      // 2. 递归处理后面层级
      deep(root.children)

      function deep(childs) {
        // 当前对象没有children
        if (childs.length === 0) return

        // 有children
        childs.forEach(obj => {
          let temp = {}
          temp.id = obj.id
          temp.name = obj.name
          temp.pid = obj.pid
          res.push(temp)
          if (obj.children.length > 0) {
            deep(obj.children)
          }
        })
      }

      return res
    }

    // 备注：id=当前结点编号  pid为父节点编号

    // tree
    let tree = {
      "id": 1,
      "name": "部门1",
      "pid": 0,
      "children": [{
        "id": 2,
        "name": "部门2",
        "pid": 1,
        "children": []
      },
      {
        "id": 3,
        "name": "部门3",
        "pid": 1,
        "children": [{
          "id": 4,
          "name": "部门4",
          "pid": 3,
          "children": [{
            "id": 5,
            "name": "部门5",
            "pid": 4,
            "children": []
          }]
        }]
      }
      ]
    }

    // Array
    let arr = [{
      id: 1,
      name: '部门1',
      pid: 0
    },
    {
      id: 2,
      name: '部门2',
      pid: 1
    },
    {
      id: 3,
      name: '部门3',
      pid: 1
    },
    {
      id: 4,
      name: '部门4',
      pid: 3
    },
    {
      id: 5,
      name: '部门5',
      pid: 4
    },
    ]

    console.log(treeToArray(tree));
  </script>
</body>

</html>